package com.lifa.controller;


import com.lifa.dao.HuiYuanChongZhiDao;
import com.lifa.entity.Huiyuan;
import com.lifa.entity.HuiyuanChongzhi;
import com.lifa.service.HuiYuanChongZhiService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.Date;

@Controller
@RequestMapping("/huiyuanChongzhi")
@Scope("prototype")
public class HuiYuanChongZhiController {

    @Autowired
    private HuiYuanChongZhiService huiYuanChongZhiService;
    @Autowired
    private HuiYuanChongZhiDao huiYuanChongZhiDao;

    @RequestMapping("/chongzhichax1")
    @ResponseBody
    public List<HuiyuanChongzhi> chongzhichax1(){
        return huiYuanChongZhiService.chongzhichax1();
    }

    @RequestMapping("/chongzhifenye1")
    @ResponseBody
    public Map<String,Object> chongzhifenye1(String username,int offset,int limit){
        Map<String,Object> map=new HashMap<>();
        map.put("rows",huiYuanChongZhiService.chongzhifneye1(username,offset,limit));
        map.put("total",huiYuanChongZhiService.chongzhizongtiaoshu1(username));
        return map;
    }

    @RequestMapping("/tianjia1")
    @ResponseBody
    public int tianjia1(HuiyuanChongzhi huiyuanChongzhi){
        return huiYuanChongZhiService.tianjia1(huiyuanChongzhi);
    }

    /**
     * 下载模板
     * 准备上传
     * @param request
     * @param resp
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/fileDownload")
    public  ResponseEntity<byte[]> fileDownload(HttpServletRequest request, HttpServletResponse resp)
            throws Exception {
        // resp.setContentType("application/vnd.ms-excel;charset=utf-8");
        byte[] body = null;
        // ServletContext servletContext = request.getServletContext();
        // String fileName = path.substring(path.lastIndexOf("_") + 1); //从uuid_name.jpg中截取文件名
        //String path = request.getSession().getServletContext().getRealPath("1.txt");
        File file = new File("充值信息.xlsx");
        InputStream in = new FileInputStream(file);
        body = new byte[in.available()];
        in.read(body);
        HttpHeaders headers = new HttpHeaders();
        String fileName = new String("充值信息.xlsx".getBytes("gbk"), "iso8859-1");
        headers.add("Content-Disposition", "attachment;filename=" + fileName);
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);

        in.close();
        return response;
    }

    @RequestMapping(value = "/fileDownloadData")
    public ResponseEntity<byte[]> fileDownloadData(HttpServletRequest request, HttpServletResponse resp)
            throws Exception {
        //加载表的所有数据
        List<HuiyuanChongzhi> list =  huiYuanChongZhiDao.chongzhichax() ;


        //在服务器上产生excel数据表格
        XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(new File("充值信息.xlsx"))) ;
        //得到excel表格的每个worksheet
        XSSFSheet sheet = xwb.getSheetAt(0);
        XSSFRow row = null;

        for (int i=0;i<list.size();i++) {
            HuiyuanChongzhi gy = list.get(i) ;
            System.out.println( " i = " + i);
            row = sheet.createRow(i+1);
            int huiyuan_Id = (int) gy.getHuiyuanchongzhi_Id();
            String huiyuan_Name = gy.getHuiyuanchognzhi_Name() ;
            int Zhongzhijine=(int)gy.getZhongzhijine();
            String chongzhi_Name=gy.getChongzhi_Name();
            String dianpu_Name=gy.getDianpu_Name();

            System.out.println(huiyuan_Id + " --- " + huiyuan_Name);


            XSSFCell cell0 = row.createCell(0) ;
            XSSFCell cell1 = row.createCell(1) ;
            XSSFCell cell2 = row.createCell(2) ;
            XSSFCell cell3 = row.createCell(3) ;
            XSSFCell cell4 = row.createCell(4) ;
            // XSSFCell cell2 = row.createCell(2) ;
            cell0.setCellValue(huiyuan_Id);
            cell1.setCellValue(huiyuan_Name);
            cell2.setCellValue(Zhongzhijine);
            cell3.setCellValue(chongzhi_Name);
            cell4.setCellValue(dianpu_Name);

        }

        FileOutputStream os = new FileOutputStream("1122咨询数据.xlsx");

        xwb.write(os);// 将文档对象写入文件输出流

        os.close();// 关闭文件输出流


        byte[] body = null;
        File file = new File("1122咨询数据.xlsx");
        InputStream in = new FileInputStream(file);
        body = new byte[in.available()];
        in.read(body);


        HttpHeaders headers = new HttpHeaders();
        String fileName = new String("1122咨询数据.xlsx".getBytes("gbk"), "iso8859-1");
        headers.add("Content-Disposition", "attachment;filename=" + fileName);
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);

        in.close();
        return response;
    }

}
